import serial
import time

grip_port = "COM5"
grip_baudrate = "9600"

def jiazhua():
    open = '45'.encode('utf-8')
    close = '90'.encode('utf-8')
    try:
        ser = serial.Serial(grip_port, grip_baudrate)
        ser.write(open)
        time.sleep(1)
        ser.write(close)
    except Exception as e:
        print("Error:", str(e))
    finally:
        if ser is not None and ser.isOpen():
            ser.close()

jiazhua()
